package com.zjhcsoft.auth.controller;

import com.dc.eai.data.Array;
import com.dc.eai.data.CompositeData;
import com.dc.eai.data.Field;
import com.dcfs.esb.utils.TLSimRepTools;
import com.zjhcsoft.auth.common.enums.ApiCode;
import com.zjhcsoft.auth.common.utils.ResultUtil;
import com.zjhcsoft.auth.common.vo.Result;
import com.zjhcsoft.auth.entity.web.PasswordPolicyRequest;
import com.zjhcsoft.auth.entity.web.RegisterAcct;
import com.zjhcsoft.auth.entity.web.RegisterChannel;
import com.zjhcsoft.auth.entity.web.RegisterRequest;
import com.zjhcsoft.auth.service.PasswordPolicyService;
import com.zjhcsoft.auth.service.RegisterService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 注册
 * @Author: wzya
 * @Date: 2021/3/22
 */
@RestController
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    RegisterService registerService;

    @Autowired
    PasswordPolicyService passwordPolicyService;
    /**
     * 注册
     * @return user_id
     */
    @PostMapping("/new")
    @ApiOperation(value="用户注册")
    public String  register(HttpServletRequest request, HttpServletResponse response) {
        String resultMsg="注册成功";
        try {
            BufferedInputStream is = new BufferedInputStream(request.getInputStream());
            CompositeData reqCD = TLSimRepTools.xmlToCD(IOUtils.toByteArray(is), "UTF-8");
            CompositeData sys = reqCD.getStruct("SYS_HEAD");
            CompositeData app = reqCD.getStruct("APP_HEAD");
            //报文解析
            RegisterRequest registerRequest = (RegisterRequest)ResultUtil.xmlToApiRequest(reqCD);
            Result result = registerService.register(registerRequest);
            result.setType(ApiCode.REGISTER.getName());
            //返回报文组装
            resultMsg = ResultUtil.getBodyForAuth(null,sys,app,result);
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return  resultMsg;
    }


    /**
     * 注册
     * @return user_id
     */
    @PostMapping("/passwordPolocyCheck")
    @ApiOperation(value="客密码复杂度校验")
    public String  passwordPolocyCheck(HttpServletRequest request, HttpServletResponse response) {
        String resultMsg="校验成功";
        try {
            BufferedInputStream is = new BufferedInputStream(request.getInputStream());
            CompositeData reqCD = TLSimRepTools.xmlToCD(IOUtils.toByteArray(is), "UTF-8");
            CompositeData sys = reqCD.getStruct("SYS_HEAD");
            CompositeData app = reqCD.getStruct("APP_HEAD");
            //报文解析
            PasswordPolicyRequest passwordPolicyRequest = (PasswordPolicyRequest)ResultUtil.xmlToApiRequest(reqCD);
            Result result = passwordPolicyService.passwordChcek(passwordPolicyRequest);
            result.setType(ApiCode.PASSWORD_POLICY_CHECK.getName());
            //返回报文组装
            resultMsg = ResultUtil.getBodyForAuth(null,sys,app,result);
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return  resultMsg;
    }

}
